package com.winchain.provider;

import com.shell.bee.base.utils.StringUtil;
import com.shell.bee.invoince.po.InvDfr;
import com.shell.bee.invoince.service.InvDfrService;
import com.shell.constant.rel.AssetCode;
import com.shell.framework.utils.MessageSender;
import com.shell.framework.utils.ParameterHelper;
import com.shell.framework.utils.SysDateUtil;
import com.shell.governor.modules.message.keyword.InvDeferredKeyword;
import com.shell.governor.modules.vo.ParameterVo;
import com.winchain.chainbill.bill.base.po.CbDct;
import com.winchain.chainbill.bill.base.po.CbRec;
import com.winchain.chainbill.bill.base.po.CbSnd;
import com.winchain.chainbill.bill.base.service.CbDctService;
import com.winchain.chainbill.bill.base.service.CbRecService;
import com.winchain.chainbill.bill.base.service.CbSndService;
import com.winchain.chainbill.tbill.base.po.TbDct;
import com.winchain.chainbill.tbill.base.po.TbRec;
import com.winchain.chainbill.tbill.base.po.TbSnd;
import com.winchain.chainbill.tbill.base.service.TbDctService;
import com.winchain.chainbill.tbill.base.service.TbRecService;
import com.winchain.chainbill.tbill.base.service.TbSndService;
import com.winchain.chainbill.trade.service.BillAcctBlcService;
import java.math.BigDecimal;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class InvoiceDeferredUploadProvider {
	private Log logger = LogFactory.getLog(getClass());
	@Autowired
	private InvDfrService invDfrService;
	@Autowired
	private CbDctService cbDctService;
	@Autowired
	private TbDctService tbDctService;
	@Autowired
	private CbRecService cbRecService;
	@Autowired
	private TbRecService tbRecService;
	@Autowired
	private BillAcctBlcService billAcctBlcService;
	@Autowired
	private TbSndService tbSndService;
	@Autowired
	private CbSndService cbSndService;

	@RequestMapping({ "/invDeferred/deferredUploadTip" })
	public void deferredUploadTip(@RequestParam("wordDate") String wordDate,
			@RequestParam("selectDfrFlag") boolean selectDfrFlag) {
		List<InvDfr> dfrList = this.invDfrService.selectDfrInvsByDateAndState(wordDate, "0", false, selectDfrFlag);
		for (InvDfr dfr : dfrList) {
			if ("1".equals(dfr.getBizType())) {
				CbDct cbDct = (CbDct) this.cbDctService.selectByKey(dfr.getBizId());
				if (cbDct != null) {
					CbRec cbRec = (CbRec) this.cbRecService.selectByKey(cbDct.getBillId());
					if ((!selectDfrFlag) && (isSendSmsTip(cbRec.getGrpId(), dfr.getApplyDate(), wordDate))) {
						sendSmsMessage(cbRec.getBillCode(), cbDct.getDctAmt().toString(), "MT07000",
								cbDct.getSellerId());
					} else if (selectDfrFlag) {
						sendSmsMessage(cbRec.getBillCode(), cbDct.getDctAmt().toString(), "MT07000",
								cbDct.getSellerId());
					}
				}
			} else if ("2".equals(dfr.getBizType())) {
				TbDct tbDct = (TbDct) this.tbDctService.selectByKey(dfr.getBizId());
				if (tbDct != null) {
					TbRec tbRec = (TbRec) this.tbRecService.selectByKey(tbDct.getBillId());
					if ((!selectDfrFlag) && (isSendSmsTip(tbRec.getGrpId(), dfr.getApplyDate(), wordDate))) {
						sendSmsMessage(tbRec.getBillCode(), tbDct.getDctAmt().toString(), "MT07003",
								tbDct.getSellerId());
					} else if (selectDfrFlag) {
						sendSmsMessage(tbRec.getBillCode(), tbDct.getDctAmt().toString(), "MT07003",
								tbDct.getSellerId());
					}
				}
			}
		}
	}

	private boolean isSendSmsTip(String grpId, String applyDate, String wordDate) {
		ParameterVo parameter = ParameterHelper.getParameter("6", "INV_TIP_DAYS", grpId);
		String days = parameter.getParmValue();
		if (!StringUtil.emptyAndNull(days)) {
			Integer bdays = SysDateUtil.getDaysBetween(applyDate, wordDate, "yyyyMMdd");
			if ((bdays != null) && (bdays.intValue() > 0)
					&& (bdays.intValue() % Integer.valueOf(days).intValue() == 0)) {
				return true;
			}
		}
		return false;
	}

	private void sendSmsMessage(String billCode, String dctAmt, String messageTplId, String entId) {
		try {
			InvDeferredKeyword keyword = new InvDeferredKeyword();
			keyword.setBillCode(billCode);
			keyword.setDctAmt(dctAmt);
			MessageSender.sendMessageByTmpl("", messageTplId, keyword, entId);
		} catch (Exception e) {
			this.logger.info("===============================" + billCode);
			this.logger.error("InvoiceDeferredUploadProvider ������������!", e);
		}
	}

	private void sendSmsMessage(String sellerName, String applyDate, String dctDays, String billCode, String dctAmt,
			String messageTplId, String entId) {
		try {
			InvDeferredKeyword keyword = new InvDeferredKeyword();
			keyword.setBillCode(billCode);
			keyword.setDctAmt(dctAmt);
			keyword.setSellerName(sellerName);
			keyword.setApplyDate(applyDate);
			keyword.setDctDays(dctDays);
			MessageSender.sendMessageByTmpl("", messageTplId, keyword, entId);
		} catch (Exception e) {
			this.logger.info("===============================" + billCode);
			this.logger.error("InvoiceDeferredUploadProvider ������������!", e);
		}
	}

	@RequestMapping({ "/invDeferred/execuDeferredUnUploadInv" })
	public void execuDeferredUnUploadInv(@RequestParam("wordDate") String wordDate) {
		List<InvDfr> dfrList = this.invDfrService.selectDfrInvsByDateAndState(wordDate, "0", true, true);
		for (InvDfr dfr : dfrList) {
			if ("1".equals(dfr.getBizType())) {
				CbDct cbDct = (CbDct) this.cbDctService.selectByKey(dfr.getBizId());
				if (cbDct != null) {
					CbRec cbRec = (CbRec) this.cbRecService.selectByKey(cbDct.getBillId());
					if (cbRec != null) {
						CbRec rootRec = (CbRec) this.cbRecService.selectByKey(cbRec.getRootId());
						int level = rootRec.getHldrLvl().intValue();
						if ((rootRec != null) && (StringUtil.emptyAndNull(rootRec.getPrntId())) && (level == 1)) {
							CbSnd cbSnd = this.cbSndService.getBillSndById(rootRec.getBillId());
							int i = this.billAcctBlcService.updateAcctBkpState(cbDct.getFinId(), cbSnd.getBuyerId(),
									AssetCode.FQT.name(), "42", "0");
							if (i == 0) {
								this.logger.info("===============================" + cbDct.getDctId() + "��������");
							} else {
								sendSmsMessage(cbDct.getSellerNm(),
										SysDateUtil.parseDate(cbDct.getDctDate(), "yyyyMMdd", "yyyy��MM��dd��"),
										cbDct.getDctDays().toString(), cbRec.getBillCode(),
										cbDct.getDctAmt().toString(), "MT07004", cbDct.getSellerId());
								sendSmsMessage(cbDct.getSellerNm(),
										SysDateUtil.parseDate(cbDct.getDctDate(), "yyyyMMdd", "yyyy��MM��dd��"),
										cbDct.getDctDays().toString(), cbRec.getBillCode(),
										cbDct.getDctAmt().toString(), "MT07004", cbSnd.getBuyerId());
							}
						}
					}
				}
			} else if ("2".equals(dfr.getBizType())) {
				TbDct tbDct = (TbDct) this.tbDctService.selectByKey(dfr.getBizId());
				if (tbDct != null) {
					TbSnd tbSnd = this.tbSndService.selectByBillId(tbDct.getBillId());
					int i = this.billAcctBlcService.updateAcctBkpState(tbDct.getFinId(), tbSnd.getBuyerId(),
							AssetCode.XTB.name(), "41", "0");
					if (i == 0) {
						this.logger.info("==n=============================" + tbDct.getDctId() + "��������");
					} else {
						TbRec tbRec = (TbRec) this.tbRecService.selectByKey(tbDct.getBillId());
						sendSmsMessage(tbDct.getSellerNm(),
								SysDateUtil.parseDate(tbDct.getDctDate(), "yyyyMMdd", "yyyy��MM��dd��"),
								tbDct.getDctDays().toString(), tbRec.getBillCode(), tbDct.getDctAmt().toString(),
								"MT07005", tbDct.getSellerId());
						sendSmsMessage(tbDct.getSellerNm(),
								SysDateUtil.parseDate(tbDct.getDctDate(), "yyyyMMdd", "yyyy��MM��dd��"),
								tbDct.getDctDays().toString(), tbRec.getBillCode(), tbDct.getDctAmt().toString(),
								"MT07005", tbSnd.getBuyerId());
					}
				}
			}
		}
	}
}
